<template>
	<view class="pd pt60 pm120">
		<view class="formrow">
			<view class="z9 fz28">
				副钥匙账户
			</view>
			<view class="mt20 pr">
				<input type="text" value="" placeholder="请输入对方账户" maxlength="11" class="jhhgerttyt" v-model="from.jhhgdfa" @input="searchuser"/>
				<image src="../../static/img/AddViceKey/kjjhfa.png" class="drrtsert" mode="widthFix" @tap="kjhjseert"></image>
			</view>
		</view>
		<view class="formrow mt70">
			<view class="z9 fz28">
				名称备注
			</view>
			<view class="mt20 pr">
				<input type="text" value="" placeholder="请设置对方备注" class="jhhgerttyt" v-model="from.jhhgdfb" />
			</view>
		</view>
		<view class="formrow mt70">
			<view class="z9 fz28">
				起止时间
			</view>
			<view class="mt20 pr jhhgerttyt row z9 dfgryusr cen">
				<view class="col fz24" @tap="jjnsd('jhhgdfc-time')">
					<text v-if="from.jhhgdfc">{{from.jhhgdfc}}</text>
					<text v-else class="z9">开始时间 </text>
				</view>
				<view class="hhgxeer">

				</view>
				<view class="col fz24"  @tap="jjnsd('jhhgdfd-time')">
					<text v-if="from.jhhgdfd">{{from.jhhgdfd}}</text>
					<text v-else class="z9">结束时间 </text>
				</view>
				<image src="../../static/img/AddViceKey/kjjhfb.png" class="drrtsert ab" mode="widthFix"></image>
			</view>
		</view>

		<view class="formrow mt70">
			<view class="z9 fz28">
				权限授权
			</view>
			<view class="mt20 pr parent">
				<view class="kjhnderty yj4 pr" :class="sd.cls" v-for="sd in sqssf" @tap="kjssf(sd)">
					<icon type="success" size="18" color="#fff" class="dsfrtyswr" />
					<view class="jjhdertt cen">
						<image :src="'../../static/img/AddViceKey/'+sd.icon" class="jhhdrettw cz" :class="sd.classname" mode="widthFix"></image>
					</view>
					<view class="fz26 cen" v-if="sd.id==3" style="color: #747474;">{{sd.name}}</view>
					<view class="cf fz26 cen" v-if="sd.id!=3">{{sd.name}}</view>
				</view>
			</view>
		</view>

		<view class="mt120 jjhhxeerts" @tap="btndj">
			<text v-if="isjihuo==0">立即激活</text>
			<text v-if="isjihuo==1">禁用副钥匙</text>
			<text v-if="isjihuo==2">激活副钥匙</text>
		</view>
		<view class="fz36 ls cen mt20" v-if="isjihuo==1||isjihuo==2" @tap="sancu">
			删除副钥匙
		</view>
		
		
		<date-time-picker ref='jhhgdfc-time' :indicatorStyle='indicatorStyle' type='datetime' :datestring='dateString' @change='dateTimeChangea'></date-time-picker>
		<date-time-picker ref='jhhgdfd-time' :indicatorStyle='indicatorStyle' type='datetime' :datestring='dateString' @change='dateTimeChangeb'></date-time-picker>
		
	</view>
</template>
<script>
	import DateTimePicker from '@/components/bory-dateTimePicker/bory-dateTimePicker.vue'
	export default {
		data() {
			return {
				userdata:[],
				num:"",
				from: {
					jhhgdfa: '',
					jhhgdfb: '',
					jhhgdfc: '',
					jhhgdfd: '',
					rulesnum:0,
					userid:0
				},
				userid:0,
				devsn:0,
				islinuser:false,
				sqssf: [{
						id:"1",
						icon: 'kjjhfc.png',
						classname: '',
						name: '开关锁',
						cls: '',
						ruleid:1,
						color:'#fff'
					},
					{
						id:"2",
						icon: 'kjjhfd.png',
						classname: 'ab',
						name: '启动熄火',
						cls: '',
						ruleid:2,
						color:'#fff'
					},
					{
						id:"3",
						icon: 'kjjhfe.png',
						classname: 'ac',
						name: '分发副钥匙',
						cls: '',
						ruleid:0,
						color:'#eee'
					},
					{
						id:"4",
						icon: 'kjjhff.png',
						classname: 'ad',
						name: '远程控制',
						cls: '',
						ruleid:4,
						color:'#fff'
					},
					{
						id:"5",
						icon: 'kjjhfg.png',
						classname: 'ae',
						name: '实时位置',
						cls: '',
						ruleid:0,
						color:'#fff'
					},
					{
						id:"6",
						icon: 'kjjhfh.png',
						classname: 'af',
						name: '临时授权',
						cls: '',
						ruleid:2000,
						color:'#fff'
					}
				],
				isjihuo: 0, // 激活的状态				
				keysid: 0 ,// 副钥匙id				
				keysauthid: 0, // 副钥匙设备授权id				
				istype: 0 // 0 新增 1 查看详情		
			}
		},
		components: {
			DateTimePicker
		},
		computed: {
			indicatorStyle() {
				return {
					background: 'rgba(0,0,0,0.1)',
					height: '40px',
				};
			}
		},
		methods: {
			jjnsd(ys){
				this.$refs[ys].show();
			},
			async searchuser(){
				let phone = this.from.jhhgdfa
				if(phone.length == 11){
					let cs = {}
					cs.keyword = phone
					cs.is_search = 1
					let jhsd = await this.post("/api/users/search", cs)
					// let user = this.$store.state.userinfo
					let userinfo = jhsd[0]
					
					if(jhsd.length>0){
						this.userid=userinfo.id
						console.log(this.userid);
					}else{
						uni.showToast({
							title:'输入账号有误'
						})
						return 
					}
				}
			},
			dateTimeChangea(e){
				this.from.jhhgdfc = e
			},
			dateTimeChangeb(e){
				this.from.jhhgdfd = e
			},
			jjcerta(e) {
				this.from.jhhgdfc = e.detail.value
			},
			jjcertb(e) {
				this.from.jhhgdfd = e.detail.value
			},
			kjssf(sd) {
				if(sd.id == 3){
					return 
				}
				if (sd.cls) {
					this.from.rulesnum -= sd.ruleid
					sd.cls = ""
					if(sd.id == 6){
						this.islinuser = false
					}
				} else {
					this.from.rulesnum += sd.ruleid
					sd.cls = "act"
					if(sd.id == 6){
						this.islinuser = true
					}
				}
				// console.log(sd.ruleid);
				// console.log(this.from.rulesnum);
			},
			kjhjseert() {
				uni.navigateTo({
					url: "/pages/leftSideMenu/ChooseFriend"
				})
			},
			async btndj() {
				if (this.isjihuo == 0||this.isjihuo == 2) {
					if(this.isjihuo==0){
						if(this.userid==0 && this.phone.length != 11){
							uni.showToast({
								title:"输入账号有误",
								icon:"none"
							})
							return
						}
						var power=this.sqssf,rules=""												
						for(var val in power){							
							if(power[val].cls){
								rules+=power[val].id+","
							}
						}
						if (rules.length > 0) {
						    rules = rules.substr(0, rules.length - 1);
						}
						// console.log("0",rules)
						let params={}
						params.type=1
						params.cid=this.carid
						params.phone=this.from.jhhgdfa
						params.name=this.from.jhhgdfb
						params.begin_time=this.from.jhhgdfc
						params.end_time=this.from.jhhgdfd
						params.rule=rules
						// 接口调用添加副钥匙
						let auths = this.from.rulesnum.toString();
						let length = 4-auths.length
						
						for(var i=0;i<length;i++){
							console.log(i)
							if(i== (length-1)){
								if(this.islinuser){
									auths = '1'+auths;
								}else{
									auths = '4'+auths;
								}
								
							}else{
								auths = '0'+auths;
							}
							
						}
						auths = parseInt(auths,16)
						// auths = '0x'+auths;
						// auths = 'Ox'+auths;
						// console.log(auths);
						let starttimestamp=((new Date(params.begin_time)).getTime()).toString()
						starttimestamp = starttimestamp.substr(0, starttimestamp.length - 3);
						let endtimestamp=((new Date(params.end_time)).getTime()).toString()
						endtimestamp = endtimestamp.substr(0, endtimestamp.length - 3);
						// console.log(starttimestamp);
						// console.log(endtimestamp);
						let param =  {"uid2":this.userid,devCode:this.devsn,power:auths,startTime:starttimestamp,endTime:endtimestamp}
						// 添加副钥匙
						 this.dqAuthAssistantKey(param,(datas)=>{
							 if(datas.success){
								 params.authid = datas.data.AuthId
								 // let activate=await this.post('/api/car_key',params) 
								 let activate=this.post('/api/car_key',params)
								 // console.log("激活",activate)			
								 uni.showModal({
								 	content: '激活成功!',
								 	showCancel:false,
								 	success: function (res) {
								 		if (res.confirm) {
								 			console.log('用户点击确定');
								 			uni.navigateBack({
								 				delta:2
								 			})
								 		} 
								 	}
								 });
								 // this.isjihuo = 1
							 }else{
								 uni.showToast({
								 	title:datas.message,
									icon:"none"
								 })
							 }
						 })
						return false 
						
						return
					}
				}
				if (this.isjihuo == 1) {
					uni.showModal({
						title: '您确定禁用该账户副钥匙吗？',
						content: '禁用后可在副钥匙详情中重新激活此 账户副钥匙功能',
						success: (e) => {
							if (e.confirm) {
								this.isjihuo = 2
							}
						}
					})
					return
				}
			},
			sancu() {
				uni.showModal({
					title: '',
					content: '您确定删除该账户副钥匙吗？',
					confirmColor: '#fC5A5A',
					confirmText: '删除'
				})
			}			
		},		
		onLoad(options) {
			console.log(options);
			
			let current = uni.getStorageSync('current')
			console.log(current);
			var th=this
			this.carid=options.carid
			this.devsn=options.devsn
			// this.devsn=current.data.devsn
			// uni.showModal({
			// 	content:JSON.stringify(current)
			// })
			// uni.showModal({
			// 	content:JSON.stringify(options)
			// })
			uni.$on('friend',(params)=>{	
				var num="",friend=""
				//num为电话号码，friend为好友名字
				for(var val in params.data){
					num+=params.data[val].phone+","	
					friend+=params.data[val].friend+","																		
				}
				if (num.length > 0) {
				    num = num.substr(0, num.length - 1);
				}
				if (friend.length > 0) {
				    friend = friend.substr(0, friend.length - 1);
				}
				console.log("传来的好友n",friend)				
				th.from.jhhgdfa=num				
			})
		},
		onUnload(){
			uni.$off('friend')
		},		
		mounted() {}
	}
</script>
<style scoped>
	
</style>
